﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace qdata
{
    public class huochenet
    {
        private static volatile huochenet m_instance = null;

        public static huochenet GetInstance()
        {
            if (m_instance == null)
            {
                lock (typeof(huochenet))
                {
                    if (m_instance == null)
                    {

                        m_instance = new huochenet();
                    }
                }
            }
            return m_instance;
        }


        public string GetHmtl(string url)
        {
             string html= Common.GetInstance().GetHtmlCode(url);
             var s1 = Regex.Matches(html, @"(?is)(<tr  class=""listBiao"">)(.*?)(</TBODY></TABLE><script)");
             if (s1==null||s1.Count<=0 || s1[0].Groups == null || s1[0].Groups.Count <= 0) return string.Empty;
             return s1[0].Groups[0].Value;
        }

        public void SaveHuocheData(string[] SArr, string[] EArr,  string site)
        {
            string url = "http://www.huoche.net/lieche/zhanzhan.php?k1={0}&k2={1}";
           
            foreach (string s1 in SArr)
            {
                foreach (string s2 in EArr)
                {

                   SaveHuocheData(string.Format(url, s1, s2), site);
                }
            }
        }

        public void SaveHuocheData(string url,string site)
        {
            string str = GetHmtl(url);

            List<string> sum = new List<string>();
            var s1 = Regex.Matches(str, @"(?is)(<tr[^>]*>)(.*?)(</tr>)");
            int i = 0;
            string sql = " insert into huochetab (checi,sstation,estation,sdate,edate,lucheng,totaldate,piaojia,piaojia2,EndSite) ";
            foreach (Match m1 in s1)
            {
                string svalue = "values('{0}','{1}','{11}','{2}','{22}','{3}','{4}','{5}','{6}','"+site+"') ";
                if (i != 0)
                {
                    var s2 = Regex.Matches(m1.Value, @"(?is)(<td[^>]*>)(.*?)(</td>)");

                    int t = 0;
                    foreach (Match m2 in s2)
                    {
                        string temp = Getstr(m2.Groups[2].Value);
                        if (t == 1 || t == 2)
                        {
                            string[] t1 = temp.Split('-');
                            if (t == 1)
                            {
                                svalue = svalue.Replace("{1}", t1[0]);
                                svalue = svalue.Replace("{11}", t1.Length < 2 ? "" : t1[1]);
                            }
                            else
                            {
                                svalue = svalue.Replace("{2}", t1[0]);
                                svalue = svalue.Replace("{22}", t1[1]);
                            }
                        }
                        else if (t == 5)
                        {
                            string[] t1 = temp.Split('/');
                            svalue = svalue.Replace("{5}", t1[0].Replace("硬座", "").Replace("一等", ""));
                            svalue = svalue.Replace("{6}",t1.Length<2?"":t1[1]);
                        }
                        else
                        {
                            svalue = svalue.Replace("{" + t.ToString() + "}", temp);
                        }
                        t++;
                    }
                    svalue = sql + svalue;
                    Common.GetInstance().ExecuteDataTable(svalue);
                    
                }

                i++;
            }
        
        }

        private string Getstr(string s)
        {
            Regex re = new Regex("</?.+?>");
            string result = re.Replace(s, "");
            return result;
        }


    }
}
